package tom.client.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;

import tom.client.DataForm;
import tom.client.FormLogin;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.client.MainPanel;
import tom.shared.AppUser;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;

public class FormAppUser extends DataForm {
	public FormAppUser() {
		txtCode.setMaxLength(8);
		lbCode.setText("ชื่อ login");

		txtFullName = new TextBox();
		txtFullName.setMaxLength(45);
		add(txtFullName, 122, 111);
		txtFullName.setSize("200px", "18px");

		Label lblNewLabel = new Label("ชื่อ - นามสกุล");
		add(lblNewLabel, 23, 111);

		txtDescript = new TextBox();
		txtDescript.setMaxLength(45);
		add(txtDescript, 122, 143);
		txtDescript.setSize("200px", "18px");

		Label label = new Label("คำอธิบาย");
		add(label, 23, 143);
		label.setSize("72px", "21px");

		txtComCode = new TextBox();
		txtComCode.setMaxLength(8);
		add(txtComCode, 122, 175);
		txtComCode.setSize("80px", "18px");
		txtComCode.addKeyDownHandler(new KeyDownHandler() {
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
					if (FormAppUser.this.getMode() == 3) {
						Window.alert("บันทึกการเพิ่มผู้ใช้ก่อน!");
					} else {
						Library.getAdminService().insertWorking(
								txtCode.getText(), txtComCode.getText(),
								delWorkCallback);
					}
			}
		});

		Label label_1 = new Label("บริษัทเริ่มต้น");
		add(label_1, 23, 175);
		label_1.setSize("72px", "21px");

		btnSearchCom = new Button("?");
		btnSearchCom.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				LookupDialog.show("บริษัท", "comCode", txtComCode);
			}
		});
		add(btnSearchCom, 216, 175);
		btnSearchCom.setSize("25px", "25px");

		listBox = new ListBox();
		add(listBox, 122, 210);
		listBox.setSize("100px", "161px");
		listBox.setVisibleItemCount(5);

		Label label_2 = new Label("บริษัทที่ทำงานได้");
		add(label_2, 23, 213);
		label_2.setSize("93px", "49px");

		btnDeleteWorking = new Button("ลบสิทธิ");
		btnDeleteWorking.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String work = listBox.getItemText(listBox.getSelectedIndex());
				if (work.equals(txtCode.getText())) {
					Window.alert(work + " เป็นบริษัทหลักของผู้ใช้ !");
				} else if (Window.confirm("ต้องการลบสิทธิการทำงานกับบริษัท "
						+ work + " หรือไม่?"))
					Library.getAdminService().deleteWorking(txtCode.getText(),
							work, delWorkCallback);

			}
		});
		add(btnDeleteWorking, 232, 210);
		btnDeleteWorking.setSize("100px", "25px");

		btnChangePwd = new Button("เปลี่ยนรหัสผ่าน");
		btnChangePwd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				FormPassword.getPanel().setParams(txtCode.getText());
				MainPanel.mainPanel.show(FormPassword.getPanel(),
						"เปลี่ยนรหัสผ่าน");
			}
		});
		add(btnChangePwd, 542, 74);
		btnChangePwd.setSize("100px", "50px");

		Label lblLevel = new Label("Level");
		add(lblLevel, 390, 147);

		comboLevel = new ListBox();
		for (String level : Library.USER_LEVEL)
			comboLevel.addItem(level);

		add(comboLevel, 427, 145);
		comboLevel.setSize("186px", "22px");
		this.setFieldsHandler();
	}

	private static FormAppUser panel;
	private AppUser record;
	private final ListBox listBox;
	private final Button btnSearchCom;
	private final Button btnDeleteWorking;
	private final Button btnChangePwd;
	private final ListBox comboLevel;

	public static FormAppUser getPanel() {
		if (panel == null)
			panel = GWT.create(FormAppUser.class);
		return panel;
	}

	@Override
	protected void search() {
		LookupDialog.show("ผู้ใช้", "userName", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getAdminService().selectMyInfo(txtCode.getText(), dataCallback);
		refreshWorking();
	}

	@Override
	protected void update() {
		getFields();
		Library.getAdminService().updateAppUser(txtCode.getText(),
				txtFullName.getText(), txtDescript.getText(),
				txtComCode.getText(), comboLevel.getSelectedIndex(),
				delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		//Window.alert(txtCode.getText());
		Library.getAdminService().createUser(txtCode.getText(),
				txtFullName.getText(), txtDescript.getText(),
				txtComCode.getText(), comboLevel.getSelectedIndex(),
				insCallback);
	}

	@Override
	protected void delete() {
		if (txtCode.getText().equals("admin")) {
			Window.alert("ไม่สามารถลบ admin ได้");
		} else if (FormLogin.frmLogin.getUser().equals(txtCode.getText())) {
			Window.alert("ไม่สามารถลบตนเองได้");
		} else {
			if (Window.confirm("ต้องการลบผู้ใช้ " + txtCode.getText()
					+ " หรือไม่")) {
				Library.getAdminService().deleteAppUser(txtCode.getText(),
						delCallback);
			}
		}

	}

	@Override
	protected void clearFields() {
		super.clearFields();
		txtCode.setText(null);
		txtFullName.setText(null);
		txtDescript.setText(null);
		txtComCode.setText(null);
		listBox.clear();
		btnChangePwd.setEnabled(false);
		btnDeleteWorking.setEnabled(false);
		btnSearchCom.setEnabled(!btnDelete.isEnabled());
		comboLevel.setSelectedIndex(-1);
		
		//Window.alert("clear field");
	}

	@Override
	protected void setFields() {
		if (record != null) {
			super.setFields();
			txtCode.setText(record.getUserName());
			txtFullName.setText(record.getFullName());
			txtDescript.setText(record.getDescript());
			txtComCode.setText(record.getComCode());
			btnChangePwd.setEnabled(true);
			btnDeleteWorking.setEnabled(true);
			btnSearchCom.setEnabled(true);
			comboLevel.setSelectedIndex(record.getLevel());
		} else {
			String code = txtCode.getText();
			clearFields();
			super.setMode(3);
			txtCode.setText(code);
		}
	}

	@Override
	protected void getFields() {
		record = new AppUser(txtCode.getText().toLowerCase(),
				txtFullName.getText(), txtDescript.getText(), null, null,
				txtComCode.getText(), comboLevel.getSelectedIndex());
	}

	@Override
	protected String getReportFile() {
		return "report002.jasper";
	}

	@Override
	protected Map<String, Object> genReportParams() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("serviceName", Window.getTitle());
		return params;
	}

	private AsyncCallback<AppUser> dataCallback = new AsyncCallback<AppUser>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(AppUser result) {
			record = result;
			setFields();
		}
	};
	private final TextBox txtComCode;
	private final TextBox txtDescript;
	private final TextBox txtFullName;

	private void refreshWorking() {
		Library.getAdminService()
				.selectWorking(txtCode.getText(), workCallback);
	}

	private AsyncCallback<List<String>> workCallback = new AsyncCallback<List<String>>() {
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
		}

		@Override
		public void onSuccess(List<String> result) {
			listBox.clear();
			for (String com : result) {
				listBox.addItem(com);
			}
		}
	};

	protected AsyncCallback<Boolean> delWorkCallback = new AsyncCallback<Boolean>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(Boolean result) {
			refreshWorking();
		}
	};
	


	
}
